package com.cxg.fairy.ec.main.personal.address;

import android.graphics.Color;
import android.graphics.MaskFilter;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;

import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.diabin.latte.ec.R;
import com.joanzapata.iconify.widget.IconTextView;

import java.util.List;

/**
 * Created by CFairy
 */

public class AddressListAdapter extends MultipleRecyclerAdapter{

    private int addressId = -1;

    public void setId(int mAddressId) {
        this.addressId = mAddressId;
    }

    protected AddressListAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(AddressItemType.ITEM_ADDRESS, R.layout.item_address_list);
    }

    @Override
    protected void convert(final MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);

        switch (holder.getItemViewType()) {
            case AddressItemType.ITEM_ADDRESS:
                final String name = entity.getField(MultipleFields.NAME);
                final String phone = entity.getField(AddressItemFields.PHONE);
                final String address = entity.getField(AddressItemFields.ADDRESS);
                boolean isSelected = false;
                final int id = entity.getField(MultipleFields.ID);
                final AppCompatTextView nameText = holder.getView(R.id.tv_address_name);
                final AppCompatTextView phoneText = holder.getView(R.id.tv_address_phone);
                final AppCompatTextView addressText = holder.getView(R.id.tv_address_address);
                final IconTextView isSelectedItv = holder.getView(R.id.itv_address_list_selected);
                final LinearLayoutCompat llAddressItem = holder.getView(R.id.ll_item_address_list);

                if (addressId > 0 && addressId == id) {
                    isSelected = true;
                } else if (addressId > 0 && addressId != id) {
                    isSelected = false;
                } else {
                    isSelected = entity.getField(AddressItemFields.IS_SELECTED);
                }

                if (isSelected) {
                    isSelectedItv.setTextColor(Color.parseColor("#02c423"));
                } else {
                    isSelectedItv.setTextColor(Color.parseColor("#ffffff"));
                }

                llAddressItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //客户端持久化选择状态
                    }
                });
                nameText.setText(name);
                phoneText.setText(phone);
                addressText.setText(address);
                break;
            default:
                break;
        }
    }

}
